import 'package:device_info/device_info.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_li_ke_jie/style/common_color.dart';
import 'package:flutter_li_ke_jie/ui/auth/real_name_model.dart';
import 'package:flutter_li_ke_jie/ui/home/home_model.dart';
import 'package:flutter_li_ke_jie/ui/login/login_model.dart';
import 'package:flutter_li_ke_jie/ui/product/product_mode.dart';
import 'package:provider/provider.dart';

import '../core/http/http.dart';
import '../core/utils/toast.dart';
import '../router/router.dart';
import '../ui/home/my_model.dart';
import '../ui/home/vip_model.dart';
import '../ui/start/start_page.dart';
import '../utils/log.dart';
import '../utils/nav_key.dart';
import '../utils/sputil.dart';
import 'dart:io' show Platform;
import 'package:uuid/uuid.dart';

import '../weight/loading_dialog.dart';
import '../weight/spin_kit_circle.dart';


class DefaultApp{
  static run(){
    // initFirst().then((value) => runApp(ToastUtils.init(const MyApp())));
    runApp(ToastUtils.init(const MyApp()));
    SystemChrome.setSystemUIOverlayStyle(
      SystemUiOverlayStyle.dark.copyWith(
        statusBarColor: Colors.transparent,//这里替换你选择的颜色
      ),
    );
    initApp();
    // _configLoading();
  }

  /// 必须要优先初始化的内容
  static Future<void> initFirst() async {
    await SPUtil.init().then((value) => getDeviceInfo());
  }

  /// 程序初始化操作
  static void initApp() async{
    XHttp.init();
    await SPUtil.init().then((value) => getDeviceInfo());

  }

  //获取设备id信息
  static void getDeviceInfo() async{
    DeviceInfoPlugin dip = DeviceInfoPlugin();
    //判断设备信息：安卓或ios
    if (Platform.isAndroid) {
      AndroidDeviceInfo adi = await dip.androidInfo;
      SPUtil.saveDevice("0"); //安卓
      String id = adi.id;
      Log.e("android设备id:$id");
      if(id.isNotEmpty){
        SPUtil.saveDeviceId(adi.id); //安卓id
      }else{
        String? deviceId = SPUtil.getDeviceId();
        if(deviceId==null||deviceId.isEmpty){
          String uuid = const Uuid().v1();
          SPUtil.saveDeviceId(uuid); //安卓id
        }
      }

    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await dip.iosInfo;
      SPUtil.saveDevice("1"); //ios
      String id = iosInfo.identifierForVendor;
      Log.e("ios设备id$id");

      if(id.isNotEmpty){
        SPUtil.saveDeviceId(id); //安卓id
      }else{
        String? deviceId = SPUtil.getDeviceId();
        if(deviceId==null||deviceId.isEmpty){
          String uuid = const Uuid().v1();
          SPUtil.saveDeviceId(uuid); //iosId
        }
      }
    }
  }
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<LoginModel>(create: (_) => LoginModel()),
        ChangeNotifierProvider<HomeModel>(create: (_) => HomeModel()),
        ChangeNotifierProvider<RealNameModel>(create: (_) => RealNameModel()),
        ChangeNotifierProvider<ProductModel>(create: (_) => ProductModel()),
        ChangeNotifierProvider<VipModel>(create: (_) => VipModel()),
        ChangeNotifierProvider<MyModel>(create: (_) => MyModel()),
      ],
      child: MaterialApp(
        onGenerateRoute:onGenerateRoute,
        routes: getRoutes(),
        initialRoute: START_PAGE,
        navigatorKey: NavKey.navKey,
        //设置主题
        theme: ThemeData(
          primaryColor:themeColor,
          buttonTheme: const ButtonThemeData(
              buttonColor:themeColor,
              textTheme: ButtonTextTheme.normal),
        ),
        builder: EasyLoading.init(),//初始化loading
        // home: const StartPage(),
      ),
    );
  }


  void _showDialog(BuildContext context){
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return LoadingDialog(
            showContent: false,
            backgroundColor: Colors.black38,
            loadingView: SpinKitCircle(color: Colors.white),
          );
        });
  }
}
